package com.dhf.framework.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.web.entity.Housecard;
import com.dhf.framework.web.entity.Person;
import com.dhf.framework.web.service.PersonService;

 /**
 * </b>PersonController<br>人员管理
 * @author HoraceHo
 * @version:1.0
 */
@Controller
@RequestMapping("/web/person")
public class PersonController extends BaseController{
	private static Logger LOGGER = LoggerFactory.getLogger(PersonController.class);
	
  	@Autowired
  	private PersonService personService;
  		
	/**
	  * Person
	  * @Title: queryPageList
	  * @Description: 列表页面
	  * @param @param searchEntity
	  * @param @param pagination
	  * @return ModelAndView    返回类型
	  * @throws
	*/
	@RequestMapping(value = "/queryPageList")
	public ModelAndView queryPageList(Person person, Pagination<Person> pagination) {
		LOGGER.debug(PersonController.class.getName()+":开始查询分页数据！");
		//创建ModelAndView对象，用来存放数据和视图。
		ModelAndView mv = new ModelAndView( "web/person/personList" );
		//调用PersonService中的方法，查询分页数据
		pagination = this.personService.queryPageList( person, pagination );
		mv.addObject( "pagination", pagination );
		return mv;
	}
		
	//点击菜单跳转到视图
	@RequestMapping(value="/queryAPersonAllHouses")
	private ModelAndView queryAPersonAllHouses(){
		//创建ModelAndView对象，用来存放数据和视图。
		ModelAndView mv = new ModelAndView( "web/person/personHouseSearch" );
		return mv;
	}
	
	
	@RequestMapping(value="/queryHouse")
	private ModelAndView queryHouse(Person person, Pagination<Person> pagination){
		LOGGER.debug(PersonController.class.getName()+":开始查询分页数据！");
		//创建ModelAndView对象，用来存放数据和视图。
		ModelAndView mv = new ModelAndView( "web/person/personHouses" );
		//调用PersonService中的方法，查询分页数据
		pagination = this.personService.queryPageList( person, pagination );
		
		List<Person> items = pagination.getItems();
		Iterator<Person> it = items.iterator();
		List<Housecard> houseCards = new ArrayList<Housecard>();
		while (it.hasNext()){
			Person p = it.next();
			houseCards = p.getHouseCards();
		}
		mv.addObject( "houseCards", houseCards );
		mv.addObject( "pagination", pagination );
		return mv;
	}
		
	/**
	 * 
	 * @Title: toMerge
	 * @Description: 新增或修改页面
	 * @param @param entity
	 * @return ModelAndView    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/toMerge")
	public ModelAndView toMerge(Person entity){
		//创建ModelAndView对象，用来存放数据和视图。
		ModelAndView mv = new ModelAndView("web/person/personMerge");
		if(entity.getId()!= null){
			Person person =  this.personService.get( entity.getId() ) ;
			mv.addObject("person",person);
		}else{
			mv.addObject("person", new Person());
		}
		return mv;
	}

	/**
	 * 
	* @Title: doMerge
	* @Description: 新增或修改数据 
	* @param @param request
	* @param @param entity
	* @return ModelAndView    返回类型
	* @throws
	 */
	@RequestMapping(value = "/doMerge")
	public ModelAndView doMerge(HttpServletRequest request, Person entity,RedirectAttributes redirectAttributes) {
		//判断应该新增数据还是修改数据。
		if (entity.getId()!=null) {
			LOGGER.debug(PersonController.class.getName()+":开始更新数据！");
			this.addMessage(redirectAttributes, "修改数据成功");
			entity.setModifyTime(new Date());
			//调用service层的方法，修改entity对象的信息。
			this.personService.update( entity );
		} else {
			LOGGER.debug(PersonController.class.getName()+":开始新增数据！");
			entity.setCreateTime(new Date());
			//调用service层的方法，新增entity对象的信息。
			this.personService.add( entity );
			this.addMessage(redirectAttributes, "新增据成功");
		}
		return new ModelAndView( "redirect:/web/person/queryPageList.do" );
	}

	/**
	 * 
	 * @Title: delete
	 * @Description:删除数据
	 * @param @param id
	 * @return ModelAndView    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/delete")
	public ModelAndView delete(Long id,RedirectAttributes redirectAttributes) {
		LOGGER.debug(PersonController.class.getName()+":开始删除数据！");
		try{
			this.personService.delete( id );
		}catch(Exception e){
			e.printStackTrace();
			this.addMessage(redirectAttributes, "该业主仍有房产！本次操作无效！！！");
			//ModelAndView mv = new ModelAndView("common/DataBaseError");
			
			return new ModelAndView( "redirect:/web/person/queryPageList.do" );
		}
        
        this.addMessage(redirectAttributes, "删除数据成功");
        return new ModelAndView( "redirect:/web/person/queryPageList.do" );
	}
	

}

